package com.sicnu.resources.controller;

import com.sicnu.resources.bean.req.logs.LogsPageReq;
import com.sicnu.resources.bean.res.Result;
import com.sicnu.resources.bean.vo.LogsVO;
import com.sicnu.resources.security.RateLimitService;
import com.sicnu.resources.service.LogsService;
import com.sun.istack.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 日志管理控制器
 * 包含日志的批量删除、根据 ID 删除、以及分页查询的接口
 * @author zhaodaowen
 */
@RestController
@Slf4j
@Validated
@RequestMapping("/api/logs")
public class LogsController {

    @Resource
    private LogsService logsService;
    // 引入 RateLimitService
    private final RateLimitService rateLimitService = new RateLimitService();

    /**
     * 分页查询
     */
    @PostMapping("/queryPage")
    public Result<List<LogsVO>> queryPage(@Valid @RequestBody LogsPageReq pageReq) {
        return logsService.queryPage(pageReq);
    }

    /**
     * 根据 ID 删除
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteById(@NotNull @PathVariable Long id) {
        String username = id.toString(); // 实现获取用户名的逻辑
        if (!rateLimitService.isAllowed(username)) {
            return Result.buildFailure("请求过于频繁，请稍后再试。");
        }
        logsService.deleteById(id);
        return Result.buildSuccess("删除成功");
    }
}